package org.pro.bookkeeping.core.service.impl;

import java.util.Date;
import java.util.List;

import org.pro.bookkeeping.core.common.ProcException;
import org.pro.bookkeeping.core.common.util.StringUtil;
import org.pro.bookkeeping.core.common.util.TransactionAspectUtil;
import org.pro.bookkeeping.core.dao.AccountDao;
import org.pro.bookkeeping.core.dao.LoggerDao;
import org.pro.bookkeeping.core.pojo.Account;
import org.pro.bookkeeping.core.pojo.Logger;
import org.pro.bookkeeping.core.service.LoggerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sun.tools.internal.ws.processor.ProcessorException;

@Transactional(rollbackFor = Exception.class)
@Service
public class LoggerServiceImpl implements LoggerService {

	@Autowired
	private LoggerDao loggerDao;
	@Autowired
	private AccountDao accountDao;

	private Integer retCode = 0;
	private String ExceptionInfo = null;

	public String addLogger(String userID, String time, String moneyValue, String type, String note, String typeID) {
		Logger logger = new Logger();

		logger.setUserID(userID);
		logger.setTime(time.equals("") || time == null ? StringUtil.simpleDateFormat.format(new Date()) : time);
		logger.setMoneyValue(moneyValue);
		logger.setType(type);
		logger.setNote(note);
		logger.setTypeID(typeID);
		try {
			loggerDao.insertLoggerOne(logger);
			Account account = new Account();
			account.setMoney(moneyValue);
			account.setUserID(userID);
			if (type.trim().equals("收入")) {
				accountDao.plusToAccount(account);
			} else if (type.trim().equals("支出")) {
				accountDao.minusToAccount(account);
			} else {
				throw new ProcessorException("没有这种type类型");
			}
		} catch (Exception e) {
			retCode = 1;
			ExceptionInfo = e.getMessage();
			e.printStackTrace();
			TransactionAspectUtil.RollbackException();
		}

		return StringUtil.WriteJson(retCode, ExceptionInfo, null);
	}
	public String delLogger(String logID) {
		try {
			loggerDao.delLogger(logID);
		} catch (Exception e) {
			retCode = 1;
			ExceptionInfo = "删除出错";
			e.printStackTrace();
			TransactionAspectUtil.RollbackException();
		}
		return StringUtil.WriteJson(retCode, ExceptionInfo, null);
	}

	public String findLoggerOneByLogID(String logID) {
		Logger logger = loggerDao.findLoggerOneByLogID(logID);
		return StringUtil.WriteJson(retCode, ExceptionInfo, logger);
	}

	public String updateLoggerOne(String userID, String time, String moneyValue, String type, String note, String typeID, String logID) {
		Logger logger = new Logger();
		logger.setUserID(userID);
		logger.setTime(time);
		logger.setMoneyValue(moneyValue);
		logger.setType(type);
		logger.setTypeID(typeID);
		logger.setLogID(logID);
		try {
			Logger l = loggerDao.findLoggerOneByLogID(logID);
			if (l == null) {
				throw new ProcException("记录不存在");
			}
			Account account = new Account();
			account.setUserID(userID);
			if (l.getType().equals("支出")) {
				if (type.equals("支出")) {
					account.setMoney((Long.parseLong(l.getMoneyValue()) - Long.parseLong(moneyValue)) + "");
				} else if (type.equals("收入")) {
					account.setMoney((Long.parseLong(l.getMoneyValue()) + Long.parseLong(moneyValue)) + "");
				} else {
					throw new ProcException("没有这种type类型");
				}
				accountDao.plusToAccount(account);
			} else {
				if (type.equals("支出")) {
					account.setMoney((Long.parseLong(l.getMoneyValue()) + Long.parseLong(moneyValue)) + "");
				} else if (type.equals("收入")) {
					account.setMoney((Long.parseLong(l.getMoneyValue()) - Long.parseLong(moneyValue)) + "");
				} else {
					throw new ProcException("没有这种type类型");
				}
				accountDao.minusToAccount(account);
			}
			loggerDao.updateLoggerOne(logger);
		} catch (Exception e) {
			retCode = 1;
			ExceptionInfo = e.getMessage();
			e.printStackTrace();
			TransactionAspectUtil.RollbackException();
		}
		return StringUtil.WriteJson(retCode, ExceptionInfo, null);
	}

	public String findLoggerListByUserID(String userID, String page, String row) {
		Logger logger = new Logger();
		logger.setUserID(userID);
		logger.setPage(page);
		logger.setRow(row);
		List<Logger> list = loggerDao.findLoggerListByUserID(logger);
		return StringUtil.WriteJson(retCode, ExceptionInfo, list);
	}
	public String cancelLogger(String logID) {
		try {
			Logger l = loggerDao.findLoggerOneByLogID(logID);
			if (l == null) {
				throw new ProcException("记录不存在");
			}
			Account account = new Account();
			account.setUserID(l.getUserID());
			account.setMoney(l.getMoneyValue());
			if (l.getType().equals("支出")) {
				accountDao.plusToAccount(account);
			} else {
				accountDao.minusToAccount(account);
			}
			loggerDao.delLogger(logID);
		} catch (Exception e) {
			retCode = 1;
			ExceptionInfo = e.getMessage();
			e.printStackTrace();
			TransactionAspectUtil.RollbackException();
		}
		return StringUtil.WriteJson(retCode, ExceptionInfo, null);
	}

}
